package com.school.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.school.common.core.domain.AjaxResult;
import com.school.common.core.domain.model.LoginUser;
import com.school.system.config.WebSocketServer;
import com.school.system.domain.Announcement;
import com.school.system.domain.Course;
import com.school.system.mapper.AnnouncementMapper;
import com.school.system.mapper.CourseMapper;
import com.school.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.system.mapper.SelectCourseMapper;
import com.school.system.domain.SelectCourse;
import com.school.system.service.ISelectCourseService;

import static com.school.common.utils.SecurityUtils.getLoginUser;
import static com.school.system.config.WebSocketServer.PROCESS_INFO;

/**
 * 选课信息表Service业务层处理
 *
 * @author fulin
 * @date 2023-03-09
 */
@Service
public class SelectCourseServiceImpl implements ISelectCourseService {
    @Autowired
    private SelectCourseMapper selectCourseMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private AnnouncementMapper announcementMapper;
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 查询选课信息表
     *
     * @param id 选课信息表主键
     * @return 选课信息表
     */
    @Override
    public SelectCourse selectSelectCourseById(Long id) {
        return selectCourseMapper.selectSelectCourseById(id);
    }

    /**
     * 查询选课信息表列表
     *
     * @param selectCourse 选课信息表
     * @return 选课信息表
     */
    @Override
    public List<Course> selectSelectCourseList(SelectCourse selectCourse) {
        LoginUser loginUser = getLoginUser();
        if (!"admin".equals(loginUser.getUsername())) {
            selectCourse.setUserId(loginUser.getUserId());
        }
        loginUser.setUserId(loginUser.getUserId());
        List<SelectCourse> selectCourseList = selectCourseMapper.selectSelectCourseList(selectCourse);
        List<Course> courses = new ArrayList<>();
        selectCourseList.forEach(data -> {
            Course course = courseMapper.selectCourseById(data.getCourseId());
            courses.add(course);
        });
        return courses;
    }

    /**
     * 新增选课信息表
     *
     * @param id 选课信息表
     * @return 结果
     */
    @Override
    public AjaxResult insertSelectCourse(String id) {
        List<SelectCourse> selectCourseList = new ArrayList<>();
        LoginUser loginUser = getLoginUser();
        Arrays.stream(id.split(",")).forEach(data -> {
            selectCourseList.add(new SelectCourse(loginUser.getUserId(), Long.valueOf(data)));
        });
        int row = selectCourseMapper.insertSelectCourse(selectCourseList);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 退回选课信息表
     *
     * @param id 选课信息表
     * @return 结果
     */
    @Override
    public AjaxResult backSelectCourse(String id) {
        LoginUser loginUser = getLoginUser();
        Arrays.stream(id.split(",")).forEach(data -> {
            Announcement announcement = Announcement.getInstance(Integer.valueOf(data), loginUser.getUserId());
            PROCESS_INFO.add(announcement);
            webSocketServer.sendMessageNow(loginUser.getUsername());
            announcementMapper.insertAnnouncement(announcement);
        });
        return AjaxResult.success();
    }


    /**
     * 审核选课信息表
     *
     * @param id 选课信息表
     * @return 结果
     */
    @Override
    public AjaxResult auditSelectCourse(String id, int status) {
        List<String> idList = Arrays.asList(id.split(","));
        selectCourseMapper.updateSelectCourseByIds(idList, status);
        return AjaxResult.success();
    }

    /**
     * 修改选课信息表
     *
     * @param selectCourse 选课信息表
     * @return 结果
     */
    @Override
    public int updateSelectCourse(SelectCourse selectCourse) {
        return selectCourseMapper.updateSelectCourse(selectCourse);
    }

    /**
     * 批量删除选课信息表
     *
     * @param ids 需要删除的选课信息表主键
     * @return 结果
     */
    @Override
    public int deleteSelectCourseByIds(Long[] ids) {
        return selectCourseMapper.deleteSelectCourseByIds(ids);
    }

    /**
     * 删除选课信息表信息
     *
     * @param id 选课信息表主键
     * @return 结果
     */
    @Override
    public int deleteSelectCourseById(Long id) {
        return selectCourseMapper.deleteSelectCourseById(id);
    }
}
